#!/bin/sh
# 记录启动目录
GIT_SYNC_PATH=`pwd`
# 记录执行日志
echo -e "[`date`] 执行{$1}同步操作" >> log.txt

# 设置其他用户执行分发操作最终用户
WEB_USER="nginx"

# 判断分发环境
if [ ! -d $1/.git ]; then
  rm -rf $1
  git clone $2 $1
  echo "版本库初始化完毕" >> $GIT_SYNC_PATH/log.txt
fi

cd $1
echo "已进入作业区" >> $GIT_SYNC_PATH/log.txt
# 清除未提交作业
git clean -f
git checkout -f
echo "作业区清理完毕" >> $GIT_SYNC_PATH/log.txt

# 拉取最新代码
git pull origin master
echo "当前版本库版本号：" >> $GIT_SYNC_PATH/log.txt
git rev-parse HEAD >> $GIT_SYNC_PATH/log.txt

# 改变用户权限
chown -R $WEB_USER:$WEB_USER $1
chmod -R 777 $1
echo "工作区操作权限交接完毕" >> $GIT_SYNC_PATH/log.txt

echo "脚本执行完毕" >> $GIT_SYNC_PATH/log.txt